package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录功能")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出功能")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * @return Result<PageResult>
     * @throws
     * @description分页查询员工的功能
     * @author Administrator
     * @param[1] employeePageQueryDTO
     * @time 2023/11/28 0:00
     */
    @ApiOperation("分页查询员工的功能")
    @GetMapping("/page")
    public Result<PageResult> pageSelectEmployee(EmployeePageQueryDTO employeePageQueryDTO) {

        log.info("正在执行根据分页查询员工的功能.......查询信息为:{}", employeePageQueryDTO);

        PageResult p = employeeService.pageSelectEmployee(employeePageQueryDTO);

        return Result.success(p);
    }

    /**
     * @return Result
     * @throws
     * @description 添加员工的功能
     * @author Administrator
     * @param[1] employeeDTO
     * @time 2023/11/28 0:01
     */
    @ApiOperation("添加员工的功能")
    @PostMapping
    public Result insertEmployee(@RequestBody EmployeeDTO employeeDTO) {

        log.info("正在执行添加员工的功能....员工信息为:{}", employeeDTO);

        employeeService.insertEmployee(employeeDTO);

        return Result.success();

    }

    /**
     * @return Result
     * @throws
     * @description 编辑员工的功能
     * @author Administrator
     * @param[1] employeeDTO
     * @time 2023/11/28 0:01
     */

    @ApiOperation("编辑员工的功能")
    @PutMapping
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO) {

        log.info("正在执行编辑员工的功能........员工信息为:{}", employeeDTO);

        employeeService.updateEmployee(employeeDTO);

        return Result.success();

    }


    /**
     * @return Result<Employee>
     * @throws
     * @description 根据id查询员工的功能
     * @author Administrator
     * @param[1] id
     * @time 2023/11/28 0:01
     */
    @ApiOperation("根据id查询员工的功能")
    @GetMapping("/{id}")
    public Result<Employee> selectByEmployeeId(@PathVariable Integer id) {

        log.info("正在执行根据id查询员工的功能.......员工id为:{}", id);

        Employee employee = employeeService.selectByEmployeeId(id);

        return Result.success(employee);

    }


    /**
     * @return Result
     * @throws
     * @description 员工账户启用及禁用的功能
     * @author Administrator
     * @param[1] status
     * @param[2] id
     * @time 2023/11/28 0:01
     */
    @ApiOperation("员工账户启用及禁用功能")
    @PostMapping("/status/{status}")
    public Result statusStartOrStop(@PathVariable Integer status, Long id) {

        log.info("正在执行根据id变更用户账户启用及禁用功能............变更的用户为:{},状态为:{}", status, id);

        employeeService.statusStartOrStop(status, id);

        return Result.success();

    }


    /**
     * @return Result
     * @throws
     * @description 员工修改密码的功能
     * @author Administrator
     * @param[1] passwordEditDTO
     * @time 2023/11/28 0:01
     */

    @ApiOperation("员工修改密码的功能")
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {

        log.info("正在执行员工修改密码的功能.........修改的密码信息为:{}", passwordEditDTO);

        employeeService.editPassword(passwordEditDTO);

        return Result.success();

    }

}
